package com.sita.linboard.calllist;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.ActivityCollector;
import com.sita.linboard.base.BaseActivity;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.Constants;
import com.sita.linboard.utils.LogUtils;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/8/24.
 * 用户叫车列表
 */
public class VehicleListActivity  extends BaseActivity implements View.OnClickListener,SwipeRefreshLayout.OnRefreshListener{
    private RecyclerView vehicleList;
    private VehicleListAdapter listAdapter;
    private LinearLayout back;
    private TextView title;
    private Button upData;
    private ProgressBar progressBar;
    private SwipeRefreshLayout refreshLayout;
    @Override
    protected void initData() {
        ActivityCollector.addActivity(this);
        listAdapter = new VehicleListAdapter(this);
        vehicleList.setLayoutManager(new LinearLayoutManager(this));
        vehicleList.setAdapter(listAdapter);

        getVehicleListData();
        title.setText("订单列表");
        upData .setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Constants.isReady||Constants.isPress==1){
            Constants.isReady = true;
        }

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        vehicleList = bindView(R.id.order_list);
        back = bindView(R.id.img_back);
        back.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        title = bindView(R.id.head_title);
        title.setVisibility(View.VISIBLE);
        upData = bindView(R.id.updata);
        progressBar = bindView(R.id.progress);
        refreshLayout = bindView(R.id.list_refresh);

    }
    @Override
    protected int getId() {

        return R.layout.activity_vehiclelist;
    }
    public static  void VehicleListIntent(Context context){
        Intent intent = new Intent(context,VehicleListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        ActivityCollector.removeActivity(this);
        super.onDestroy();
    }
    @Override
    protected void onPause() {

        super.onPause();
    }

    private void getVehicleListData(){
        VehicleRequest request = new VehicleRequest();
        request.page="0";
        request.size ="10";
        request.driverId = SpUtils.getString("AccoundID",null, BaseApplication.getContext());;
        RestClient.getRestService().orderList(request, new Callback<VehicleListBackBean>() {
            @Override
            public void success(VehicleListBackBean vehicleListBackBean, Response response) {
                listAdapter.getData(vehicleListBackBean,progressBar);
            }
            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.updata:
                if (BaseApplication.isNetworkAvailable()){
                    getVehicleListData();
                }
                else {
                    ToastUtils.show("网络异常",3);
                }
                break;
            case R.id.img_back:
              finish();
                break;
        }
    }

    @Override
    public void onRefresh() {
        getVehicleListData();
        refreshLayout.setRefreshing(false);
    }
}
